package cn.com.pet.administrator.action.system;

import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import cn.com.pet.common.base.BaseAction;
import cn.com.pet.common.base.ErrorMessage;
import cn.com.pet.common.entity.AdminUser;
import cn.com.pet.common.entity.Privilege;
import cn.com.pet.common.service.AdminUserService;
import cn.com.pet.common.service.PrivilegeService;
import cn.com.pet.common.util.MD5Util;

@Namespace("/action")
@ParentPackage("base")
@SuppressWarnings({ "serial" })
public class SystemAction extends BaseAction {

	@Autowired
	AdminUserService adminUserService;

	@Autowired
	PrivilegeService privilegeService;

	@Action(value = "login", results = {
			@Result(name = SUCCESS, type = "redirectAction", location = "pageIndex"),
			@Result(name = ERROR, location = "/html/administrator/login.jsp")
	})
	public String login() {
		String userName = this.getRequest().getParameter("username");
		String password = this.getRequest().getParameter("password");
		String verifyCode = this.getRequest().getParameter("verifycode");
		String code = this.session.get("rand").toString();
		if (!verifyCode.equals(code)) {
			this.getRequest().setAttribute("error", ErrorMessage.ADMIN_VCODE_ERROR);
			return ERROR;
		}

		if (null ==  userName || userName.equals("")) {
			this.getRequest().setAttribute("error", ErrorMessage.ADMIN_USER_NULL);
			return ERROR;
		}

		if (null == password || password.equals("")) {
			this.getRequest().setAttribute("error", ErrorMessage.ADMIN_PASS_NULL);
			return ERROR;
		}
		AdminUser adminUser = new AdminUser();
		adminUser.setAdminName(userName);
		AdminUser adminUserResult = adminUserService.adminUserLogin(adminUser);

		if (null == adminUserResult) {
			this.getRequest().setAttribute("error", ErrorMessage.ADMIN_NAME_ERROR);
			return ERROR;
		}

		if (!MD5Util.validateMD5(password, adminUserResult.getAdminPassword())) {
			this.getRequest().setAttribute("error", ErrorMessage.ADMIN_PASS_ERROR);
			return ERROR;
		}

		this.getSession().put("adminUser", adminUserResult);
		return SUCCESS;
	}

	@Action(value = "pageIndex", results = {
			@Result(name = SUCCESS, location = "/html/administrator/index.jsp"),
			@Result(name = ERROR, location = "/html/administrator/login.jsp") })
	public String pageIndex() {
		Object userInfo = this.getSession().get("adminUser");
		if (null == userInfo) {
			this.getRequest().setAttribute("error", ErrorMessage.ADMIN_LOGIN_NULL);
			return ERROR;
		}
		return SUCCESS;
	}

	@Action(value = "pageLeft", results = {
			@Result(name = SUCCESS, location = "/html/administrator/left.jsp") })
	public String pageLeft() {
		List<Privilege> privilegeList = privilegeService.getAllPrivilege();
		this.request.setAttribute("privilegeList", privilegeList);
		return SUCCESS;
	}

	@Action(value = "pageRight", results = {
			@Result(name = SUCCESS, location = "/html/administrator/right.jsp") })
	public String pageRight() {
		return SUCCESS;
	}

	@Action(value = "pageTop", results = {
			@Result(name = SUCCESS, location = "/html/administrator/top.jsp") })
	public String pageTop() {

		return SUCCESS;
	}

	@Action(value = "logout", results = {
			@Result(name = SUCCESS, location = "/html/administrator/login.jsp") })
	public String pageLogin() {
		return SUCCESS;
	}
}
